// pages/device/warnList.ts
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDrawer: false,
    showCalendar: false,
    scrollHeight: 0,
    dateRange: {},
    filterCount: 3,
    warnCount: 0,
    warnList: [],
    filterCate: [
      { label: '全部', value: '0' },
      { label: '故障', value: '1' },
      { label: '可疑', value: '2' },
      { label: '良好', value: '3' },
      { label: '离线', value: '4' },
    ],
    filterList: [
      {
        cateName: '幼儿园项目',
        cateList: [{
          name: '西昌之心幼儿园',
          isActive: true,
        }, {
          name: '外国语幼儿园',
          isActive: false,
        }, {
          name: '川美幼儿园',
          isActive: true,
        }, {
          name: '蓝天幼稚园',
          isActive: false,
        }, {
          name: '春田幼儿园',
          isActive: false,
        }, {
          name: '东路幼儿园',
          isActive: true,
        },]
      }
    ]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.calcScrollHeight()
    this.setData({
      filterList: this.generateFilterList()
    });
    this.getWarnList()
  },
  getWarnList() {
    app.axios('device', {
      op: "ajaxGbwarrList",
    }).then(res => {
      if (res.code) {
        this.setData({
          warnCount: res.lists.length
        })
      }
      if (res.lists && res.lists.length > 0) {
        const resultList = []
        res.lists.forEach(item => {
          const warnItem = {
            id: item.id,
            location: item.rname,
            title: item.rname,
            problem: item.title,
            dateTime: app.dateFormatter((item.addtime) * 1000)
          }
          resultList.push(warnItem)
        })
        this.setData({
          warnList: resultList
        })
      }
    })
  },
  calcScrollHeight() {
    const query = wx.createSelectorQuery();
    // 选择导航栏和筛选框元素
    query.select('.nav').boundingClientRect();
    query.select('.filterBox').boundingClientRect();

    query.exec((res) => {
      if (res && res[0] && res[1]) {
        const navHeight = res[0].height;    // 导航栏高度
        const filterHeight = res[1].height; // 筛选框高度
        const windowHeight = wx.getWindowInfo().windowHeight; // 屏幕高度

        // 计算剩余高度，确保非负数
        const scrollHeight = Math.max(0, windowHeight - navHeight - filterHeight);

        this.setData({ scrollHeight });
      }
    });
  },

  toggleDrawer() {
    // 直接调用组件方法（需获取实例）
    const drawer = this.selectComponent('#v-drawer')
    drawer.toggleDrawer()
  },

  onDrawerChange(e) {
    console.log('抽屉状态:', e.detail.visible)
  },

  onDrawerClose() {
    this.setData({ showDrawer: false })
  },
  showCalendar() {
    this.setData({ showCalendar: true });
  },
  onCloseCalendar() {
    this.setData({ showCalendar: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirmCalendar(event) {
    const [start, end] = event.detail;
    this.setData({
      showCalendar: false,
      // date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
      dateRange: { startDate: this.formatDate(start), endDate: this.formatDate(end) }
    });
  },
  generateFilterList() {
    // 从现有数据中提取所有名称作为样本
    const originalNames = this.data.filterList[0].cateList.map(item => item.name);

    // 创建包含4个分类的新筛选列表
    const newFilterList = [];
    for (let i = 0; i < 4; i++) {
      const cate = {
        cateName: `筛选分类 ${i + 1}`,  // 分类名称
        cateList: []
      };

      // 为每个分类创建8个子项
      for (let j = 0; j < 8; j++) {
        cate.cateList.push({
          name: originalNames[Math.floor(Math.random() * originalNames.length)], // 随机选取名称
          isActive: Math.random() > 0.5 // 随机设置激活状态
        });
      }
      newFilterList.push(cate);
    }
    return newFilterList;
  },
  navigateTo(e) {
    const { url } = e.currentTarget.dataset
    app.navigateTo({
      url: url,
    })
  }
})